/**
 * 2017年8月23日
 */
package exp.core;

import java.io.FileWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;

import exp.algorithm.sic.ScalePeak;
import exp.algorithm.sic.TimeSeries;
import exp.api.InstancesConverter;
import exp.api.TemplateSaver;
import exp.entity.ChartDataset;
import freemarker.template.Configuration;
import freemarker.template.Template;
import weka.core.Instances;

/**
 * 读取html模板
 * @author Alex
 *
 */
//@Component
public class OutFileWriter implements TemplateSaver{
	@Value("#{setting[template_save_dir]}")
	String dir ;
	final String suffix = ".html";
	@Autowired Configuration cfg;
	@Autowired InstancesConverter<ChartDataset> ic;
	@Override
	public void save(Instances inst,String saveName,String tName) throws Exception {
		Template t = cfg.getTemplate(tName);
		InstancesConverter<ChartDataset> ic = new JavaScriptArrayInstancesConverter();
		ChartDataset res = ic.process(inst);
		Map<String,Object> f = Maps.newHashMap();
		f.put("dataset", res);
		Writer w = new FileWriter(FileUtils.getFile(dir,saveName.endsWith(".html")?saveName:(saveName+suffix)));
		t.process(f, w);
		w.close();
	}
	@Override
	public void save(String saveName, String templateName, TimeSeries original, TimeSeries[] smoothed,
			ScalePeak[] peaks) throws Exception {
	}

}
